<template>
  <div class="bargin">
    <!-- 议价 -->
    <el-descriptions title="询价信息" :labelStyle="{ width: '100px' }" :column="2">
      <el-descriptions-item label="型号">
      <DescriptionsTooltip :value="qutoData.inquiryProduct.partnumber">{{ qutoData.inquiryProduct.partnumber }}</DescriptionsTooltip>
      </el-descriptions-item>
      <el-descriptions-item label="品牌">
       <DescriptionsTooltip :value="qutoData.inquiryProduct.brand">{{ qutoData.inquiryProduct.brand }}</DescriptionsTooltip>
      </el-descriptions-item>
      <el-descriptions-item label="数量">{{
        qutoData.inquiryQuantity
      }}</el-descriptions-item>
      <el-descriptions-item label="批次">{{
        qutoData.inquiryProduct.dateCode
      }}</el-descriptions-item>
      <el-descriptions-item label="封装">
      <DescriptionsTooltip :value="qutoData.inquiryProduct.package">{{  qutoData.inquiryProduct.package }}</DescriptionsTooltip>
      </el-descriptions-item>
      <el-descriptions-item label="接受价">{{
        Number.isInteger(qutoData.costPrice) ? qutoData.costPrice.toFixed(2) : qutoData.costPrice
      }}</el-descriptions-item>
      <el-descriptions-item label="币种">{{
        qutoData.inquiryCurrency == "CNY" ? "人民币" : "美元"
      }}</el-descriptions-item>
      <el-descriptions-item label="交货日期">{{
        qutoData.inquiryDeliveryDate
      }}</el-descriptions-item>
      <el-descriptions-item label="交货地">{{
        qutoData.inquiryDeliveryPlace == "FArea01" ? "中国大陆" : "中国香港"
      }}</el-descriptions-item>
      <el-descriptions-item label="包装">
      <DescriptionsTooltip :value="qutoData.inquiryProduct.packing">{{ qutoData.inquiryProduct.packing }}</DescriptionsTooltip>
    </el-descriptions-item>
      <el-descriptions-item label="发票类型">
      <DescriptionsTooltip :value=" qutoData.inquiryInvoiceType">{{ qutoData.inquiryInvoiceType }}</DescriptionsTooltip>
      </el-descriptions-item>
      <el-descriptions-item label="" style="opacity: 0;"></el-descriptions-item>
    </el-descriptions>
    <label class="suptitle">报价信息</label>
    <el-descriptions title="" :labelStyle="{ width: '100px' }" :column="2">
      <el-descriptions-item label="我方报价">{{
        Number.isInteger(qutoData.inPrice) ? qutoData.inPrice.toFixed(2) : qutoData.inPrice
      }}</el-descriptions-item>
      <el-descriptions-item label="币种">{{
        qutoData.currency
      }}</el-descriptions-item>
      <el-descriptions-item label="报价数量">{{
        qutoData.quantity
      }}</el-descriptions-item>
      <el-descriptions-item label="交货日期">{{
        qutoData.deliveryDate
      }}</el-descriptions-item>
      <el-descriptions-item label="批次">{{
        qutoData.product_dateCode
      }}</el-descriptions-item>
      <el-descriptions-item label="交货地">{{
        qutoData.deliveryPlace == "FArea01" ? "中国大陆" : "中国香港"
      }}</el-descriptions-item>
    </el-descriptions>
    <label class="suptitle">议价信息</label>
    <el-form :model="ruleForm" :rules="rules" ref="form" label-width="100" label-position="left">
      <el-form-item label="" class="isAccept">
        <el-checkbox v-model="isAccept" @change="handleAccept" name="quto">
          一键同意
        </el-checkbox>
      </el-form-item>
      <el-form-item label="币种：" prop="currency">
        <el-select v-model="ruleForm.currency" @change="changeOptions($event)" class="input">
          <el-option label="人民币" value="1"></el-option>
          <el-option label="美元" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="议价价格：" prop="unitPrice">
        <!-- <el-input-number :controls="false" v-inpNum :max="999999999" :precision="6"
              v-model="ruleForm.unitPrice">></el-input-number> -->
        <el-input v-model="ruleForm.unitPrice" @blur="handleBlur" @input="handleInput"></el-input>
      </el-form-item>
      <el-form-item label="报价数量：" prop="quantity">
        <el-input-number :controls="false" :max="999999999" :precision="0" v-inpNum.noquto
          v-model="ruleForm.quantity"></el-input-number>
      </el-form-item>
      <el-form-item label="交货日期：" prop="deliveryDate">
        <el-date-picker v-model="ruleForm.deliveryDate" :picker-options="pickerOption" type="date"
          value-format="yyyy-MM-dd" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报价有效期：" prop="termDate" v-show="false">
        <el-date-picker v-model="ruleForm.termDate" :picker-options="pickerOption" type="date" value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="交货地：" prop="deliveryPlace">
        <el-select v-model="ruleForm.deliveryPlace" class="place">
          <el-option label="中国大陆" value="FArea01"></el-option>
          <el-option label="中国香港" value="FArea02"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注：" prop="summary" class="summary">
        <el-input v-model="ruleForm.summary" placeholder="请输入内容" class="desc">
        </el-input>
      </el-form-item>
    </el-form>
    <div class="foot" v-loading="btnLoading">
      <el-button type="primary" size="small" @click="handleConfirmQuto">
        确认议价</el-button>
      <!-- <el-button plain size="small" @click="$emit('close')">取消</el-button> -->
      <el-button class="resletBtn" size="small" @click="handleNotQuto">不支持议价</el-button>
    </div>
  </div>
</template>
<script>
import DescriptionsTooltip from "@/components/DescriptionsTooltip/index.vue";
import { Bargain, UnableBargain } from "@/api/scsapi/index";
import { mapGetters } from "vuex";
import { nextDate } from "@/utils";
export default {
  name: "bargin",
  components: {
    DescriptionsTooltip
  },
  data() {
    return {
      isAccept: false,
      btnLoading: false,
      ruleForm: {
        unitPrice: undefined,
        currency: undefined,
        quantity: undefined,
        deliveryDate: undefined,
        deliveryPlace: undefined,
        summary: undefined,
        termDate: nextDate(),
      },

      pickerOption: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      rules: {
        unitPrice: [{ required: true, message: "请输入价格", trigger: "blur" }],
        currency: [{ required: true, message: "请输入价格", trigger: "change" }],
        quantity: [{ required: true, message: "请输入数量", trigger: "blur" }],
        deliveryDate: [
          { required: true, message: "请选择交货日期", trigger: "change" },
        ],
        deliveryPlace: [
          { required: true, message: "请选择交货地", trigger: "change" },
        ],
        termDate: [
          { required: true, message: "请选择报价有效期", trigger: "change" },
        ],
      },
    };
  },
  props: {
    qutoData: {
      default: () => {
        return {};
      },
      type: Object,
      required: true,
    },
  },
  created() {
    // console.log('this.qutoData', this.qutoData)
    // this.ruleForm.currency = this.qutoData.currency
    // this.ruleForm.deliveryDate = this.qutoData.deliveryDate;
    // this.ruleForm.deliveryPlace = this.qutoData.deliveryPlace;
  },
  computed: {
    ...mapGetters(["id"]),
  },
  mounted() {
    console.log('this.qutoData', this.qutoData)
    this.ruleForm.currency = this.qutoData.currency
    this.ruleForm.deliveryDate = this.qutoData.deliveryDate;
    this.ruleForm.deliveryPlace = this.qutoData.deliveryPlace;
  },
  methods: {
    handleInput() {
      const value = this.ruleForm.unitPrice.trim();
      const reg = /^\d*$/;
      if (!reg.test(value)) {
        this.ruleForm.unitPrice = value.replace(/[^\d.]/g, '');
      }
    },
    handleBlur() {
      const value = this.ruleForm.unitPrice.trim();
      if (/^\d+$/.test(value)) {
        this.ruleForm.unitPrice = value + '.00';
      }
    },
    // 选中人民币就选中国内
    changeOptions(parsms) {
      console.log('tag', parsms)
      if (parsms == 1) {
        this.ruleForm.deliveryPlace = 'FArea01'
      } else {
        this.ruleForm.deliveryPlace = 'FArea02'
      }
    },
    handleAccept() {
      // unitPrice 报价
      // currency  币种
      // quantity 报价数量
      // deliveryDate 交货日期
      // dateCode 批次
      // deliveryPlace  交货地
      // termDate 报价有效期
      // supplierID 报价公司
      if (this.isAccept) {
        this.$set(
          this.ruleForm,
          "unitPrice",
          this.qutoData.costPrice ? this.qutoData.costPrice.toFixed(2): 0
        );
        this.$set(this.ruleForm, "currency", this.qutoData.currency);
        this.$set(this.ruleForm, "quantity", this.qutoData.quantity);
        this.$set(this.ruleForm, "deliveryDate", this.qutoData.deliveryDate);
        // this.$set(this.ruleForm, "dateCode", this.qutoData.dateCode);
        this.$set(this.ruleForm, "deliveryPlace", this.qutoData.deliveryPlace || this.qutoData.district);
      } else {
        this.$refs["form"].resetFields();
      }
    },
    handleConfirmQuto() {
      this.btnLoading = true;
      this.$refs.form.validate((valid) => {
        if (valid) {
          let params = { id: this.qutoData.id, ...this.ruleForm };
          Bargain(params).then((res) => {
            if (res.status == 200) {
              this.$message.success("操作成功");
              this.$emit("close");

              this.$store.dispatch("sidebars/getInquiryTotal", {
                userId: this.id
              });
            }
            this.btnLoading = false;
          });
        } else {
          this.btnLoading = false;
        }
      });
    },

    // 无法报价
    handleNotQuto() {
      this.btnLoading = true;
      this.$confirm("确认无法议价?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          UnableBargain({
            ids: [this.qutoData.id],
            reason: "",
          }).then((res) => {
            if (res.status == 200) {
              this.$message.success("操作成功");
              this.$emit("close");
              this.$store.dispatch("sidebars/getInquiryTotal", {
                userId: this.id
              });
            }
            this.btnLoading = false;
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
          });
          this.btnLoading = false;
        });
    },
  },
  directives: {
    inpNum: {
      inserted: function (el, binding) {
        el.oninput = function (v) {
          setTimeout(() => {
            let value = v.target.value;
            if (binding.modifiers.noquto) {
              value = value.replace(/[^\d]+/g, "");
            } else {
              value = value.replace(/[^\d^\.]+/g, "");
            }
            v.target.value = value;
          });
        };
      },
    },
  },
};
</script>
<style scoped lang="scss" src="@/assets/styles/public.scss"></style>

<style scoped lang="scss">
.bargin {
  position: relative;
}

::v-deep .el-descriptions__header {
  color: #1A2035;
  font-weight: 500;
  font-size: 14px;
  margin-bottom: 15px;

  // .el-descriptions__title{
  //   color: #1A2035;
  //   font-weight: 500;
  //   font-size: 14px;
  // }
  .el-descriptions__title {
    font-size: 14px;
  }
}

::v-deep .el-descriptions__body {
  // margin: 0 20px;
  border: 1px solid #ccc;
  padding: 12px 20px;

  .el-descriptions-item__container {
    justify-content: space-between !important;
  }

  .el-descriptions-item__label {
    color: #999;
    font-size: 12px;
  }

  .el-descriptions-item__content {
    color: #1A2035;
    font-size: 12px;
    font-weight: 400;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

}

.suptitle {
  color: #1A2035;
  display: block;
  font-weight: 600;
  font-size: 14px;
  padding: 15px 0;
}

.el-form {
  border: 1px solid #ccc;
  padding: 12px 20px;

  .el-checkbox {
    width: 110px;
    height: 20px;
    line-height: 20px;
    background: #2348E8;
    border-radius: 2px;
    color: #fff;
    text-align: center;

    ::v-deep.el-checkbox__label {
      font-size: 12px !important;
      color: #fff !important;
    }

    ::v-deep.el-checkbox__input.is-checked .el-checkbox__inner {
      // background-color: #fff !important;
    }
  }

  ::v-deep.el-form-item__label {
    font-size: 12px;
    color: #999;
  }

  // margin: 0 20px;


  // ::v-deep .el-input,
  // .el-select {
  //   width: 250px;
  // }
  .input {
    margin-left: 24px;
  }

  .el-select--small {
    width: 84%;
  }

  .el-input--small {
    width: 84%;
  }

  .el-input-number--small {
    width: 84%;
  }

  .isAccept {
    ::v-deep .el-form-item__content {
      margin-left: 0px !important;
    }
  }

  .place {
    margin-left: 13px;
  }

  ::v-deep .el-form-item__error {
    // top: 10px;
    left: 68px;
  }

  .el-form-item {
    margin-bottom: 15px;
  }

  ::v-deep .el-input__inner {
    text-align: left;
  }

  .summary {
    ::v-deep.el-form-item__label {
      margin-left: 8px !important;
    }

    .desc {
      margin-left: 24px;
    }
  }
}

.foot {
  margin: 10px 100px;
  display: flex;
  justify-content: center;
  padding: 10px 20px;
}</style >
 